package com.ruoyi.web.controller.community.cpc;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.community.domain.BaseActivityRegisterRequest;
import com.ruoyi.community.domain.CpcActivity;
import com.ruoyi.community.domain.CpcActivityEnroll;
import com.ruoyi.community.domain.CpcActivityParticipate;
import com.ruoyi.community.service.ICpcActivityParticipateService;
import com.ruoyi.community.service.ICpcActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 党建活动签到Controller
 * 
 * @author ruoyi
 * @date 2021-12-13
 */
@RestController
@RequestMapping("/community/cpc/participate")
public class CpcActivityParticipateController extends BaseController
{
    @Autowired
    private ICpcActivityParticipateService CpcActivityParticipateService;

    @Autowired
    private ICpcActivityService CpcActivityService;

    /**
     * 签到
     */
    @Log(title = "签到", businessType = BusinessType.INSERT)
    @PostMapping("/register")
    public AjaxResult register(@RequestBody BaseActivityRegisterRequest baseActivityRegisterRequest)
    {
        Integer result = CpcActivityParticipateService.insertCpcActivityParticipate(baseActivityRegisterRequest);
        String errorInfo;
        if (result==-1){
            errorInfo = "您已签到，请勿重复签到！";
        }else{
            errorInfo = "签到验证码错误！";
        }
        return toAjax(result, errorInfo);
    }

    /**
     * 查询党建活动签到列表
     */
//    @PreAuthorize("@ss.hasPermi('community:participate:list')")
    @GetMapping("/list")
    public TableDataInfo list(CpcActivityParticipate CpcActivityParticipate)
    {
        startPage();
        List<CpcActivityParticipate> list = CpcActivityParticipateService.selectCpcActivityParticipateList(CpcActivityParticipate);
        return getDataTable(list);
    }

    // 根据活动id查询签到列表
    @GetMapping("/list/{activityId}")
    public AjaxResult listParticipateById(@PathVariable("activityId") String activityId)
    {
        List<SysUser> userList = CpcActivityParticipateService.selectCpcActivityParticipateListById(activityId);
        CpcActivityParticipate query = new CpcActivityParticipate();
        query.setActivityId(activityId);
        List<CpcActivityParticipate> participateList = CpcActivityParticipateService.selectCpcActivityParticipateList(query);
        ArrayList<Object> list = new ArrayList<>();
        for (int i = 0; i < participateList.size(); i++) {
            Map enrollDict = new HashMap<>();
            enrollDict.put("record", participateList.get(i));
            enrollDict.put("userInfo", userList.get(i));
            list.add(enrollDict);
        }
        return AjaxResult.success(list);
    }

    /**
     * 查询某人签到的所有活动
     */
    @GetMapping("/getMyParticipate")
    public AjaxResult listParticipateByUserId()
    {
        CpcActivityParticipate cpcActivityParticipate = new CpcActivityParticipate();
        cpcActivityParticipate.setUserId(SecurityUtils.getUserID());
        List<CpcActivityParticipate> list = CpcActivityParticipateService.selectCpcActivityParticipateList(cpcActivityParticipate);
        List<CpcActivity> resultList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            CpcActivity cpcActivity = CpcActivityService.selectCpcActivityById(list.get(i).getActivityId());
            resultList.add(cpcActivity);
        }
        return AjaxResult.success(resultList);
    }

    /**
     * 导出党建活动签到列表
     */
//    @PreAuthorize("@ss.hasPermi('community:participate:export')")
    @Log(title = "党建活动签到", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CpcActivityParticipate CpcActivityParticipate)
    {
        List<CpcActivityParticipate> list = CpcActivityParticipateService.selectCpcActivityParticipateList(CpcActivityParticipate);
        ExcelUtil<CpcActivityParticipate> util = new ExcelUtil<CpcActivityParticipate>(CpcActivityParticipate.class);
        return util.exportExcel(list, "党建活动签到数据");
    }

    /**
     * 获取党建活动签到详细信息
     */
//    @PreAuthorize("@ss.hasPermi('community:participate:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(CpcActivityParticipateService.selectCpcActivityParticipateById(id));
    }

    /**
     * 修改党建活动签到
     */
//    @PreAuthorize("@ss.hasPermi('community:participate:edit')")
    @Log(title = "党建活动签到", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CpcActivityParticipate CpcActivityParticipate)
    {
        return toAjax(CpcActivityParticipateService.updateCpcActivityParticipate(CpcActivityParticipate));
    }

    /**
     * 删除党建活动签到
     */
//    @PreAuthorize("@ss.hasPermi('community:participate:remove')")
    @Log(title = "党建活动签到", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(CpcActivityParticipateService.deleteCpcActivityParticipateByIds(ids));
    }
}
